package org.linlinjava.litemall.wx;

import org.junit.Test;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.sql.Date;

/**
 * @author zhy
 * 原子操作
 * @date 2019-06-10 15:40
 **/
public class AtomicAdd {
	static AtomicLong num = new AtomicLong(0);

	public static void main(String args[]) throws InterruptedException {
		CountDownLatch ctl = new CountDownLatch(10);    // 倒计数器
		for (int i = 0; i < 10; i++) {
			new Thread() {
				@Override
				public void run() {
					for (int j = 0; j < 10000000; j++) {
						num.getAndIncrement();
					}
					ctl.countDown();    // 计数器减一
				}
			}.start();
		}
		ctl.await();    // 设置倒计数开关，当计数变为 0 时，开关打开
		System.out.println(num.get());
	}

	@Test
	public  void main2() {
		java.util.Date datetime=new java.util.Date();
		java.sql.Timestamp time=new java.sql.Timestamp(datetime.getTime());
		System.out.println(time);
	}

	@Test
	public  void main6() {
//		String a  = "32.01";
//		long as =Long.valueOf(a).longValue();
//		System.out.println(as);

		String strTime = "12.5416132";
		double dblTime = Double.parseDouble(strTime);

//		long lngTime = new Double(dblTime).longValue();
		System.out.println(dblTime);

	}

	@Test
	public void main5(){
		BigDecimal a = new BigDecimal(0.01);
		BigDecimal b = new BigDecimal(0.01);
		System.out.println((a.add(b)).setScale(2, BigDecimal.ROUND_HALF_UP));

	}
	@Test
	public void  main3(){
		long me = 3500;
		BigDecimal bigDecimal = new BigDecimal(0.00);
		long meter = 20000;
		if(me>meter){
			long lastMeter = me - meter;
			String num = String.valueOf(lastMeter);
			Integer a = Integer.parseInt(num.substring(0,1));
			a = a*10;
			BigDecimal otherFee = new BigDecimal(a);
			bigDecimal = bigDecimal.add(otherFee);
		}
		System.out.println(bigDecimal);
	}


	@Test
	public void main4() {
		String strDate = "2017-06-19 14:39:28"; // <input type="datetime-local"> 输入参数
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date = null;
		     try {
			            date = sdf.parse(strDate);
			   } catch (ParseException e) {
			            e.printStackTrace();
			    }
		    java.sql.Timestamp dateSQL = new java.sql.Timestamp(date.getTime());
		System.out.println(dateSQL);
		//			stu.setCreateTime(new Timestamp(dt.getTime()));


		/**
		 * 将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存
		 * @param strDate
		 *            表示日期的字符串
		 * @param dateFormat
		 *            传入字符串的日期表示格式（如："yyyy-MM-dd HH:mm:ss"）
		 * @return java.sql.Timestamp类型日期对象（如果转换失败则返回null）
		 */
//		public static java.sql.Timestamp strToSqlDate(String strDate, String dateFormat) {
//			SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
//			java.util.Date date = null;
//			try {
//				date = sf.parse(strDate);
//			} catch (ParseException e) {
//				e.printStackTrace();
//			}
//			java.sql.Timestamp dateSQL = new java.sql.Timestamp(date.getTime());
//			return dateSQL;
//		}

	}
}
